import { last } from "lodash-es";
import mitt from "mitt";
import { Toast } from "vant";
import { useControllerStore } from "../Controller/Controller";
import { KeyBind } from "./keyAndMouse";
import { TouchBind } from "./TouchBind";

export const UserHub = mitt<{
    recover: string;
    cancel: string;
    save: string;
    scale: number;

    move: undefined;
    moving: undefined;
    stopMove: undefined;
}>();

export const isPC = window.ontouchstart ? false : true;
export const UserEvent = isPC ? new KeyBind() : new TouchBind();
/** 统一管理的，没有鼠标与触控之分的事件 */
export const UserInput = {
    cancel() {
        const store = useControllerStore();
        if (store.selectedLayer.length) {
            UserHub.emit("cancel", last(store.selectedLayer)!.id);
            Toast({ message: "撤销", position: "bottom" });
        }
    },
    recover() {
        const store = useControllerStore();
        if (store.selectedLayer.length) {
            UserHub.emit("recover", last(store.selectedLayer)!.id);
            Toast({ message: "重做", position: "bottom" });
        }
    },
    save() {
        UserHub.emit("save", "");
        Toast({ message: "保存", position: "bottom" });
    },
};
